/**
 * 用户相关类型定义
 */
export interface User {
  id: number | string
  username?: string | null
  name?: string | null
  eMail?: string | null
  roleId?: number | null
  role?: string | null // 前端显示用的角色名称
  permissions?: string[]
  pagePrivilege?: number // 页面权限码
  subPagePrivilege?: number // 子页面权限码
  nmcPrivilegeIds?: string // NMC权限ID
  avatar?: string
  phone?: string
  orgId?: number
  department?: string // 前端显示用的部门名称
  isEnable?: boolean
  password?: string
  createdAt?: string
  updatedAt?: string
}

/**
 * 登录相关类型
 */
export interface LoginForm {
  loginName: string
  loginPassword: string
  name:string
  password: string
  remember?: boolean
}

export interface UserRoleInfo {
  id: number
  name: string
}

export interface RoleFuncPrivilege {
  id: number
  roleId: number
  pagePrivilege: number
  subPagePrivilege: number
  nmcPrivilegeIds: string
}

export interface LoginResponseData {
  accessToken: string
  accessTokenExpires: string
  refreshToken: string
  refreshTokenExpires: string
  id: string
  name: string
  userRoleInfo: UserRoleInfo
  roleFuncPrivilege: RoleFuncPrivilege
  roleCameraPrivilegelst: any[]
}

export interface RefreshTokenResponseData {
  accessToken: string
  refreshToken: string
  accessTokenExpires: string
}

export interface LoginResponse {
  code: number
  msg: string
  data: LoginResponseData
}

/**
 * 菜单相关类型
 */
export interface MenuItem {
  id: number
  name: string
  path: string
  icon?: string
  component?: string
  parentId?: number
  sort: number
  permissions: string[]
  hidden: boolean
  children?: MenuItem[]
}

/**
 * 设备目录相关类型
 */
export interface DeviceMenu {
  id: number
  name: string
  parentId: number
  children?: DeviceMenu[]
  devCount: number
}

/**
 * 地图设备标记相关类型
 */
export interface MapDeviceMark {
  cancelSet: boolean
  devId: number
  longitude: string
  latitude: string
}

/**
 * 摄像头相关类型
 */
export interface Camera {
  id: number
  name: string
  code: string
  ip: string
  port: number
  username: string
  password: string
  rtspUrl: string
  hlsUrl?: string
  status: 'online' | 'offline' | 'error'
  longitude: number
  latitude: number
  location: string
  description?: string
  createdAt: string
  updatedAt: string
}

/**
 * 视频录像相关类型
 */
export interface VideoRecord {
  id: number
  cameraId: number
  filename: string
  startTime: string
  endTime: string
  duration: number
  size: number
  url: string
  status: 'recording' | 'completed' | 'error'
}

/**
 * 地图相关类型
 */
export interface MapPoint {
  id: string
  longitude: number
  latitude: number
  height?: number
  name: string
  type: 'camera' | 'building' | 'equipment' | 'alarm'
  data?: any
}

/**
 * API 响应类型
 */
export interface ApiResponse<T = any> {
  code: number
  msg: string
  data: T
  count?: number
  success?: boolean
  timestamp?: number
}

 // 分页相关类型

export interface PageQuery {
  page: number
  pageSize: number
  keyword?: string
  start?: string
  end?: string
}
export interface PageResponse<T = any> {
  data: T[]
  total: number
  page: number
  pageSize: number
  pages: number
}

/**
 * 角色相关类型
 */
export interface Role {
  roleId: number
  roleName: string
  pagePrivilege?: string
  subPagePrivilege?: string
  nmcPrivilege?: string
  cameraPrivileges?: CameraPrivileges[]
}

export interface CameraPrivileges {
  cameraId: number
  name: string
  nvrId: number
  ptzEnable: boolean
  subEquId: number
}

/**
 * 树形节点类型
 */
export interface TreeNode {
  id: number
  name: string
  parentId: number
  children?: TreeNode[]
  devices?: Device[]
  devCount?: number
}

/**
 * 报警信息类型
 */
export interface Alarm {
  id: string
  deviceName: string
  alarmType: string
  level: 'critical' | 'major' | 'minor'
  message: string
  createTime: string
  createUserId: string
  alarmContent: string
  alarmTime: string
  deleteTime: string
  deleteUserId: string
  deviceId: number
  deviceType: number
  idDelete: boolean
  nvrId: number
  
}

/**
 * 设备类型 - 匹配API返回格式
 */
export interface Device {
  id: number
  gbId: string
  nvrId: number
  menuId: number
  equId: number
  subEquId: number
  name: string
  ip: string
  port: number
  protocol: string
  posionType: number
  latitude: string
  longitude: string
  hls: string
  hlssub: string
  flv: string
  flvsub: string
  status: number  // 0: 离线, 1: 在线
  ptzEnable: boolean  // 是否支持云台控制
  mapDevice?: MapDevice  // 地图设备信息（如果设备在地图中标记）
}

/**
 * 地图设备类型
 */
export interface MapDevice {
  id: number
  name: string
  longitude: number
  latitude: number
  flv:string
  flvsub:string
  hls:string
  hlssub:string
  nvrName:string
  nvrId:number
  ip:string
  status:number
}

/**
 * 流媒体服务器信息
 */
export interface StreamServer {
  id?: number
  ip: string
  port: number
  name?: string
  type?: number
  online?: boolean
  uuid?: string
  username?: string
  password?: string
  streamPort?: number
}

/**
 * 组织架构相关类型
 */
export interface Organize {
  id: number
  name: string
  code: string
  parentId?: number
  level: number
  sort: number
  type: 'department' | 'group' | 'team'
  description?: string
  manager?: string
  phone?: string
  email?: string
  address?: string
  status: 'active' | 'inactive'
  children?: Organize[]
  deviceCount?: number
  userCount?: number
  createdAt: string
  updatedAt: string
}

/**
 * 组织架构查询参数
 */
export interface OrganizeQuery extends PageQuery {
  parentId?: number
  type?: string
  status?: string
  keyword?: string
}

/**
 * 组织架构树节点
 */
export interface OrganizeTreeNode {
  id: number
  name: string
  abbreviation: number
  code: string
  parentId?: number
  children?: OrganizeTreeNode[]
}

/**
 * 页面权限码常量 - 对应后端LIMIT_PAGE_MASK
 */
export const LIMIT_PAGE_MASK = {
  LIMIT_PAGE_MASK_PREVIEW: 1,        // 实时视频
  LIMIT_PAGE_MASK_MAP: 2,            // 电子地图
  LIMIT_PAGE_MASK_CAMERA_SET: 4,     // 摄像机设置
  LIMIT_PAGE_MASK_VIDEO_REPLAY: 8,   // 录像回放
  LIMIT_PAGE_MASK_PAGE: 16,          // 菜单管理
  LIMIT_PAGE_MASK_ORG: 32,           // 部门管理
  LIMIT_PAGE_MASK_ROLE: 64,          // 角色管理
  LIMIT_PAGE_MASK_NVR: 128,          // 流媒体管理
  LIMIT_PAGE_MASK_NMC: 256,          // NMC管理
  LIMIT_PAGE_MASK_USER: 512,         // 用户管理
  LIMIT_PAGE_MASK_ALARM: 1024,       // 报警管理
} as const

/**
 * 页面权限配置
 */
export interface PagePermission {
  id: string
  name: string
  code?: number
  path: string
  icon: string
  children?: PagePermission[]
  disabled?: boolean // 是否禁用（默认选中且不允许取消）
  isConfigurable?: boolean // 是否需要配置权限
}

/**
 * 完整的菜单权限配置数据（按侧边栏结构）
 */
export const MENU_PERMISSIONS: PagePermission[] = [
  {
    id: 'video',
    name: '视频监控',
    path: '/video',
    icon: 'VideoCamera',
    children: [
      {
        id: 'preview',
        name: '实时预览',
        code: LIMIT_PAGE_MASK.LIMIT_PAGE_MASK_PREVIEW,
        path: '/video/live',
        icon: 'VideoPlay',
        isConfigurable: true
      },
      {
        id: 'map',
        name: '电子地图',
        code: LIMIT_PAGE_MASK.LIMIT_PAGE_MASK_MAP,
        path: '/video/map',
        icon: 'MapLocation',
        isConfigurable: true
      },
      {
        id: 'camera-set',
        name: '摄像机设置',
        code: LIMIT_PAGE_MASK.LIMIT_PAGE_MASK_CAMERA_SET,
        path: '/video/camera',
        icon: 'Camera',
        isConfigurable: true
      },
      {
        id: 'video-replay',
        name: '录像回放',
        code: LIMIT_PAGE_MASK.LIMIT_PAGE_MASK_VIDEO_REPLAY,
        path: '/video/playback',
        icon: 'VideoPause',
        isConfigurable: true
      }
    ]
  },
  {
    id: 'alarm',
    name: '报警系统',
    code: LIMIT_PAGE_MASK.LIMIT_PAGE_MASK_ALARM,
    path: '/alarm',
    icon: 'Bell',
    isConfigurable: true
  },
  {
    id: 'system',
    name: '系统管理',
    path: '/system',
    icon: 'Setting',
    children: [
      {
        id: 'user',
        name: '用户管理',
        code: LIMIT_PAGE_MASK.LIMIT_PAGE_MASK_USER,
        path: '/system/user',
        icon: 'User',
        isConfigurable: true
      },
      {
        id: 'role',
        name: '角色管理',
        code: LIMIT_PAGE_MASK.LIMIT_PAGE_MASK_ROLE,
        path: '/system/role',
        icon: 'UserFilled',
        isConfigurable: true
      },
    ]
  },
  {
    id: 'device',
    name: '设备管理',
    path: '/device',
    icon: 'Monitor',
    children: [
      {
        id: 'nvr',
        name: '流媒体管理',
        code: LIMIT_PAGE_MASK.LIMIT_PAGE_MASK_NVR,
        path: '/device/stream',
        icon: 'VideoCamera',
        isConfigurable: true
      },
      {
        id: 'nmc',
        name: 'NMC管理',
        code: LIMIT_PAGE_MASK.LIMIT_PAGE_MASK_NMC,
        path: '/device/nmc',
        icon: 'Setting',
        isConfigurable: true
      }
    ]
  }
]

/**
 * 页面权限配置数据（仅包含需要配置的页面）
 */
export const PAGE_PERMISSIONS: PagePermission[] = [
  {
    id: 'preview',
    name: '实时视频',
    code: LIMIT_PAGE_MASK.LIMIT_PAGE_MASK_PREVIEW,
    path: '/video/live',
    icon: 'VideoPlay'
  },
  {
    id: 'map',
    name: '电子地图',
    code: LIMIT_PAGE_MASK.LIMIT_PAGE_MASK_MAP,
    path: '/video/map',
    icon: 'MapLocation'
  },
  {
    id: 'camera-set',
    name: '摄像机设置',
    code: LIMIT_PAGE_MASK.LIMIT_PAGE_MASK_CAMERA_SET,
    path: '/video/camera',
    icon: 'Camera'
  },
  {
    id: 'video-replay',
    name: '录像回放',
    code: LIMIT_PAGE_MASK.LIMIT_PAGE_MASK_VIDEO_REPLAY,
    path: '/video/playback',
    icon: 'VideoPause'
  },
  {
    id: 'role',
    name: '角色管理',
    code: LIMIT_PAGE_MASK.LIMIT_PAGE_MASK_ROLE,
    path: '/system/role',
    icon: 'UserFilled'
  },
  {
    id: 'nvr',
    name: '流媒体管理',
    code: LIMIT_PAGE_MASK.LIMIT_PAGE_MASK_NVR,
    path: '/device/stream',
    icon: 'VideoCamera'
  },
  {
    id: 'nmc',
    name: 'NMC管理',
    code: LIMIT_PAGE_MASK.LIMIT_PAGE_MASK_NMC,
    path: '/device/nmc',
    icon: 'Setting'
  },
  {
    id: 'user',
    name: '用户管理',
    code: LIMIT_PAGE_MASK.LIMIT_PAGE_MASK_USER,
    path: '/system/user',
    icon: 'User'
  },
  {
    id: 'alarm',
    name: '报警管理',
    code: LIMIT_PAGE_MASK.LIMIT_PAGE_MASK_ALARM,
    path: '/alarm',
    icon: 'Bell'
  }
]

/**
 * Tree V2 树形节点类型
 */
export interface TreeV2Node {
  id: string | number
  label: string
  children?: TreeV2Node[]
  disabled?: boolean
  isLeaf?: boolean
  data?: any
}

/**
 * 通道树形节点类型（用于Tree V2）
 */
export interface ChannelTreeNode extends TreeV2Node {
  id: string
  label: string
  type: 'group' | 'camera'
  parentId?: string
  children?: ChannelTreeNode[]
  disabled?: boolean
  isLeaf?: boolean
  data?: {
    originalData: any
    checked?: boolean
    status?: 'online' | 'offline' | 'error'
    ip?: string
    port?: number
  }
}

/**
 * NVR告警类型映射
 */
export const NVR_ALARM_TYPE_MAP = {
  0x00000004: '移动检测',
  0x00000008: '视频丢失',
  0x00000010: '镜头遮挡',
  0x00000020: '输入报警',
  0x00000040: '手动报警',
  0x00000080: '电力报警',
  0x00000081: '设备断线',
  0x00000082: '子设备掉线',
  0x00000083: '磁盘离线',
  0x00000084: '存储内存配置报警',
  0x00000085: '网口速度报警',
  0x00000086: '设备上线',
  0x00000087: '子设备上线',
  0x00000088: '设备开机连接就连上',
  0x00000089: '设备开机连接就没连接上',
  0x00000099: 'AI报警'
} as const

/**
 * 告警类型工具函数
 */
export const getAlarmTypeText = (alarmType: string | number): string => {
  const typeValue = typeof alarmType === 'string' ? parseInt(alarmType, 10) : alarmType
  return NVR_ALARM_TYPE_MAP[typeValue as keyof typeof NVR_ALARM_TYPE_MAP] || `未知告警类型(${alarmType})`
}

/**
 * 摄像机云台控制
 */
export interface PtzControl {
  nvrId: number
  subEquId: number
  command?: number
  speed?: number
}

export interface StreamPort {
  realStreamPort: string
  recordStreamPort: string
}

/**
 * 录像查询相关类型
 */
export interface PlaybackQueryParams {
  EquId: number
  nvrId: number
  ChannelId: number
  StartTime: string
  EndTime: string
}

export interface PlaybackRecord {
  id: string
  startTime: string
  endTime: string
  duration: number
  filePath: string
  fileSize: number
  type: 'normal' | 'alarm' | 'motion'
  status: 'available' | 'unavailable' | 'recording'
  // 录像服务器原始数据字段
  index?: number
  equId?: number
  subEquId?: number
  channel?: number
}

export interface PlaybackQueryResponse {
  records: PlaybackRecord[]
  total: number
  hasMore: boolean
}

/**
 * 回放流地址相关类型
 */
export interface PlaybackStreamIdParams {
  token: string
}

export interface PlaybackStreamIdResponse {
  code: number
  message: string
  stream_id: string
}

/**
 * 回放token生成参数
 */
export interface PlaybackTokenParams {
  deviceId?: number  // d后默认是0
  subEquId: number   // c后是子设备号
  startTime: string  // s是开始时间 (ISO格式)
  endTime: string    // e是结束时间 (ISO格式)
}